<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script src="./1.js"></script>
    <script>

        // 罗马数字转阿拉伯数字


        function getNum (letter) {
            switch (letter) {
                case 'I': 
                    return 1;
                case 'V': 
                    return 5;
                case 'X': 
                    return 10;
                case 'L': 
                    return 50;
                case 'C': 
                    return 100;
                case 'D': 
                    return 500;
                case 'M': 
                    return 1000;
            
                default:
                    return 0;
            }
        }

        function getSpecialNum (doubleLetter) {
            switch (doubleLetter) {
                case 'IV': 
                    return 4;
                case 'IX': 
                    return 9;
                case 'XL': 
                    return 40;
                case 'XC': 
                    return 90;
                case 'CD': 
                    return 400;
                case 'CM': 
                    return 900;
            
                default:
                    return 0;
            }
        }

        // switch处理映射关系
        function roman2int2 (roman) {
            let sum = 0;
            // 循环roman字符串 拿出每一个罗马字母
            for (let i = 0; i < roman.length; i++) {
                const letter = roman[i]; // I V
                const double = roman.substring(i, i + 2);

                if (getSpecialNum(double)) {
                    sum += getSpecialNum(double);
                    // 跳过下次循环
                    i++;
                } else {
                    sum += getNum(letter);
                }
            }
            return sum;
        }

        // 对象处理映射关系
        function roman2int1 (roman) {
            // 映射关系
            const mapping = {
                I: 1,
                V: 5,
                X: 10,
                L: 50,
                C: 100,
                D: 500,
                M: 1000,
            };
            // 特殊的映射关系
            const special = {
                IV: 4,
                IX: 9,
                XL: 40,
                XC: 90,
                CD: 400,
                CM: 900,
            };
            let sum = 0;
            // 循环roman字符串 拿出每一个罗马字母
            for (let i = 0; i < roman.length; i++) {
                const letter = roman[i]; // I V
                const double = roman.substring(i, i + 2);

                if (special[double]) {
                    sum += special[double];
                    // 跳过下次循环
                    i++;
                } else {
                    sum += mapping[letter];
                }
            }
            return sum;
        }

        // 性能测试函数 callback >> cb
        function p (name, cb, count = 1000000) {
            console.time(name);
            for (let i = 0; i < count; i++) {
                cb();
            }
            console.timeEnd(name);
        }


        p('对象方式', () => {
            roman2int1('IV');
            roman2int1('LVIII');
            roman2int1('MCMXCIV');
        });

        p('switch方式', () => {
            roman2int2('IV');
            roman2int2('LVIII');
            roman2int2('MCMXCIV');
        });


    </script>
</body>
</html>